package org.stephen.recommend.model;

import org.stephen.recommend.enums.LanguageType;
import org.stephen.recommend.entity.Article;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * ClassName: HotNewsList
 * Description:
 * Date: 2020/12/17 10:59
 *
 * @author stephen.qiu
 */
public class HotNewsList {
    private static HotNewsList hotNewsList = new HotNewsList();

    private List<Article> enArticleList = null;

    private List<Article> zhArticleList = null;

    private List<String> enArticleIdsList = null;

    private List<String> zhArticleIdsList = null;

    private LocalDateTime lastInitTopHotNewsListTime = null;

    private HotNewsList() {
    }

    public static HotNewsList getInstance() {
        return hotNewsList;
    }

    public void setArticleList(List<Article> list) {
        this.zhArticleList = list;
        zhArticleIdsList = list.stream().map(c -> c.getId()).collect(Collectors.toList());
    }

    public void setArticleList(List<Article> list, LanguageType languageType) {
        if (languageType.equals(LanguageType.EN_US)) {
            this.enArticleList = list;
            enArticleIdsList = list.stream().map(c -> c.getId()).collect(Collectors.toList());
        } else {
            this.zhArticleList = list;
            zhArticleIdsList = list.stream().map(c -> c.getId()).collect(Collectors.toList());
        }
    }

    public List<Article> getArticleList(LanguageType languageType) {
        return languageType.equals(LanguageType.EN_US) ? enArticleList : zhArticleList;
    }

    public List<String> getArticleIdsList(LanguageType languageType) {
        return languageType.equals(LanguageType.EN_US) ? enArticleIdsList : zhArticleIdsList;
    }

    public List<Article> getArticleList() {
        return zhArticleList;
    }

    public LocalDateTime getLastInitTopHotNewsListTime() {
        return lastInitTopHotNewsListTime;
    }

    public void setLastInitTopHotNewsListTime(LocalDateTime lastInitTopHotNewsListTime) {
        this.lastInitTopHotNewsListTime = lastInitTopHotNewsListTime;
    }

    public Boolean isExist(String articleId, LanguageType languageType) {
        return getArticleIdsList(languageType).contains(articleId);
    }

    /**
     * 随机获取num篇文章
     * @param num
     * @param languageType
     * @return
     */
    public List<Article> getRandomArticles(int num, LanguageType languageType) {
        List<Article> articlesList = new LinkedList<>();
        if (num >= getArticleList(languageType).size()) {
            return getArticleList(languageType);
        }
        List<Integer> indexList = getIndexList(num, getArticleList(languageType).size());
        indexList.forEach((c) -> articlesList.add(getArticleList(languageType).get(c)));
        return articlesList;
    }

    /**
     * 从0-max中获取num个不相同的数字
     * @param num
     * @param max
     * @return
     */
    private List<Integer> getIndexList(int num, int max) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            list.add(i);
        }
        Random mRandom = new Random();
        int index;
        for (int i=0;i<list.size();i++) {
            index = mRandom.nextInt(max);
            int temp = list.get(i);
            list.set(i,list.get(index));
            list.set(index,temp);
        }
        return list.subList(0,num);
    }
}
